const http = require('http')
const fs = require('fs')
const url = require('url')
const mimeData = require('./data/mime.json')
const path = require('path')
let newsdata = require('./data/data.json')
let server = http.createServer((req, res) => {
    let pathObj = url.parse(req.url, true)
    let pathname = pathObj.pathname
    if (pathname == '/index' || pathname == '/') {
        //新闻列表
        let data = fs.readFileSync('./views/index.html')
        res.write(data)
        res.end()
    } else if (pathname == '/detail') {
        //详情列表
        let data = fs.readFileSync('./views/detail.html')
        res.write(data)
        res.end()

    } else if (pathname == 'getdata') {
        //资源处理
        if (req.url !== "/favicon.ico") {
            res.write(JSON.stringify(newsdata))
            res.end()
        } else {
            let extname = path.extname(req.url)
            res.setHeader("Content-Type", mimeData[extname])


            let data = fs.readFileSync('./static/' + req.url)
            res.write(data)
            res.end()

        }
    }
})
server.listen(8787)